import Toast from '@vant/weapp/toast/toast';
import {
  regionData
} from '../../static/regionData'
import {
  myGet,
  myPost
} from '../../request/index'
import {
  auth_phone,
  submitUserInfo,
  getRecommendUserInfo,
  getCarInfoTree
} from '../../request/api'
const app = getApp()
Page({
  data: {
    customBar: app.globalData.statusBarHeight + 44,
    showRegion: false,
    showCarType: false,
    argeePolicy: false,
    formData: {
      name: '',
      phone: '',
      carType: '',
      carTypeCode: '',
      carBrand: '',
      carBrandCode: '',
      cityCode: '',
      cityName: '',
      provinceCode: '',
      provinceName: ''
    },
    autoSize: {
      maxHeight: 120,
      minHeight: 120
    },
    recommendUserInfo: {

    },
    regionValue: '',
    regionOptions: regionData,
    carTypeValue: '',
    carTypeOptions: []
  },
  onLoad({
    shareId
  }) {
    const recommendForm = wx.getStorageSync('recommendForm')
    if (recommendForm) {
      this.setData({
        'formData': recommendForm.formData,
        regionValue: recommendForm.regionValue,
        carTypeValue: recommendForm.carTypeValue
      })
    }
    if (shareId) {
      this.setData({
        'formData.shareId': shareId
      })
    }
  },
  getData() {
    this.getCarInfoTree()
    this.getRecommendUserInfo()
  },
  onRegionClick() {
    this.setData({
      showRegion: true
    })
  },
  getCarInfoTree() {
    myGet(getCarInfoTree, {}, (res) => [
      this.setData({
        carTypeOptions: res.data
      })
    ])
  },
  getRecommendUserInfo() {
    myGet(getRecommendUserInfo, {
      shareId: this.data.formData.shareId
    }, (res) => {
      if (res.status === 200) {
        this.setData({
          recommendUserInfo: res.data
        })
      }
    })
  },
  getPhoneNumber(e) {
    console.log(e)
    const code = e.detail.code
    if (!code) {
      Toast.fail("手机号获取权限被禁用")
      return
    }
    Toast.loading({
      message: '获取中...',
      forbidClick: true,
      duration: 0
    });
    myGet(auth_phone, {
      code: code
    }, (res) => {
      Toast.clear()
      if (res.status === 200) {
        this.setData({
          "formData.phone": res.data
        })
      }
    })
  },
  finishRegion(e) {
    const {
      selectedOptions,
    } = e.detail;
    const fieldValue = selectedOptions
      .map((option) => option.text || option.name)
      .join('/');
    this.setData({
      regionValue: fieldValue,
      'formData.cityCode': selectedOptions[0].value,
      'formData.cityName': selectedOptions[0].text,
      'formData.provinceCode': selectedOptions[1].value,
      'formData.provinceName': selectedOptions[1].text,
      showRegion: false
    })
  },
  cancelRegion() {
    this.setData({
      showRegion: false
    })
  },
  onCarTypeClick() {
    this.setData({
      showCarType: true
    })
  },
  onNameInputChange(event) {
    this.setData({
      'formData.name': event.detail
    })
  },
  onRemarkInputChange(event) {
    this.setData({
      'formData.remark': event.detail
    })
  },
  finishCarType(e) {
    const {
      selectedOptions,
    } = e.detail;
    const fieldValue = selectedOptions
      .map((option) => option.text || option.name)
      .join('/');
    this.setData({
      carTypeValue: fieldValue,
      'formData.carBrand': selectedOptions[0].text,
      'formData.carBrandCode': selectedOptions[0].value,
      'formData.carType': selectedOptions[1].text,
      'formData.carTypeCode': selectedOptions[1].value,
      showCarType: false
    })
  },
  cancelCarType() {
    this.setData({
      showCarType: false
    })
  },
  toPolicy() {
    wx.navigateTo({
      url: '/pages/policy/index',
    })
  },
  onPolicyChange(e) {
    this.setData({
      argeePolicy: e.detail
    })
  },
  assertArgeePolicy() {
    const valid = this.validFormRequired()
    if (!valid) {
      return
    }
    if (!this.data.argeePolicy) {
      wx.showModal({
        title: '温馨提示',
        content: '您是否已阅读并同意隐私政策？',
        complete: (res) => {
          if (res.confirm) {
            this.setData({
              argeePolicy: true
            })
            this.submitForm()
            return
          }
          Toast.fail("请先阅读并同意隐私政策后再提交！")
        }
      })
      return
    }
    this.submitForm()
  },
  validFormRequired() {
    const {
      name,
      phone,
      carBrand,
      carType,
      cityName,
      provinceName
    } = this.data.formData
    if (!name) {
      Toast("请输入姓名")
      return false
    }
    if (!phone) {
      Toast("请获取手机号")
      return false
    }
    if (!carBrand || !carType) {
      Toast("请选择意向车型")
      return false
    }
    if (!cityName || !provinceName) {
      Toast("请选择省市")
      return false
    }
    return true
  },
  submitForm() {
    Toast.loading({
      message: '提交中',
      forbidClick: true,
      duration: 0
    });
    myPost(submitUserInfo, this.data.formData, (res) => {
      if (res.status == 200) {
        Toast.success("提交成功")
        wx.removeStorageSync('recommendForm')
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/user/index',
          })
        }, 2000)
        return
      }
      wx.setStorageSync('recommendForm', {
        carTypeValue: this.data.carTypeValue,
        regionValue: this.data.regionValue,
        formData: this.data.formData
      })
    })
  },
  onPageScroll(e) {
    const navigationBar = this.selectComponent("#navigationBar")
    navigationBar.onScroll(e.scrollTop)
  },
})